<?php

namespace app\common\model;

use think\Model;


class BrowseLog extends Model
{


    // 表名
    protected $name = 'browse_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text'
    ];


    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    /**
     * Account:写入浏览记录
     * User: Ixiangang
     * Date: 2025/2/11
     * Time: 16:34
     */
    public function addBrowse($user_id, $ordinary_id, $type)
    {
        # 浏览咨询师
        if ($type == 1) {
            $info = self::where(['user_id' => $user_id, 'goods_id' => $ordinary_id])->find();
        }
        # 浏览测评项目
        if ($type == 2) {
            $info = self::where(['user_id' => $user_id, 'project_id' => $ordinary_id])->find();
        }
        if (empty($info)) {
            self::create([
                'user_id' => $user_id,
                'goods_id' => $type == 1 ? $ordinary_id : 0,
                'project_id' => $type == 2 ? $ordinary_id : 0,
                'type' => $type,
                'createtime' => time()
            ]);
        }
        return true;
    }


}
